package com.qfedu.openplateform.manager.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.Recharge;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.RechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/sys/recharge")
public class RechargeController {

    @Autowired
    private RechargeService rechargeService;

    @RequestMapping("/table")
    public TableData<Recharge> table(Integer page,Integer limit){

        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(rechargeService.getRechargeList());
        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }
    @RequestMapping("/update")
    public AjaxMessage update(Recharge recharge) {
        try {
            Date date = new Date();
            recharge.setUpdatetime(date);
            rechargeService.updateRecharge(recharge);
            return new AjaxMessage(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "修改失败");

    }

    @RequestMapping("del")
    public AjaxMessage del(Integer[] ids) {
        try {
            rechargeService.deleteRecharge(ids);
            return new AjaxMessage(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "删除失败");
    }

    @RequestMapping("add")
    public AjaxMessage add(Recharge recharge) {
        try {
            Date date = new Date();
            recharge.setCreatetime(date);
            rechargeService.addRecharge(recharge);
            return new AjaxMessage(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new AjaxMessage(false, "添加失败");
    }


}
